var  baseUrl = 'http://192.168.0.24:8080';

// 路径配置
require.config({
    paths: {
        echarts: 'echarts-2.2.7/build/dist/'
    }
});
// 使用
var shmj = function (id, mjdata, type) {
    if (mjdata == null) {
        return;
    }
    switch (type) {
        case "bar":
            shmjbar(id, mjdata);
            break;
        case "line":
            shmjbar(id, mjdata);
            break;
        case "pie":
            shmjpie(id, mjdata);
            break;
        case "stack":
            shmjbar(id, mjdata);
            break;
        default:
            break;
    }
}
var shmjbar = function (id, mjdata) {
    require(
        [
            'echarts',
            'echarts/chart/bar', // 使用柱状图就加载bar模块，按需加载
            'echarts/chart/line',
        ],
        function (ec) {
            // 基于准备好的dom，初始化echarts图表
            var myChart = ec.init(document.getElementById(id));

            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    x: 30,
                    x2: 10,
                    y: 80,
                    y2: 30,
                },
                legend: {
                    data: get_line(mjdata),
                },
                toolbox: {
                    show: true,
                    x: 'right',
                    y: '50',
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: mjdata.y_line
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: mjdata.group,
            };
            // 为echarts对象加载数据
            myChart.setOption(option);
        }
    );
}
var shmjpie = function (id, mjdata) {

    require(
        [
            'echarts',
            'echarts/chart/pie'
        ],
        function (ec) {
            // 基于准备好的dom，初始化echarts图表
            var myChart2 = ec.init(document.getElementById(id));
            //设置数据
            var option2 = {
                title: {
                    x: 'center'
                },
                tooltip: {
                    background: 'red',
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    data: get_pie_line(mjdata),
                },
                calculable: true,
                series: [
                    mjdata,
                ]
            };
            myChart2.setOption(option2);
        }
    );
}

var get_line = function (mjdata) {
    var shen = [];
    for (var j = 0; j < mjdata.group.length; j++) {
        var f = mjdata.group[j].name;
        shen.push(f);
    }
    return shen;
}
var get_pie_line = function (mjdata) {
    var shen = [];
    for (var j = 0; j < mjdata.data.length; j++) {
        var f = mjdata.data[j].name;
        shen.push(f);
    }
    return shen;
};
//var test = function () {
//    // for(var j=0;j<shu.length;j++){
//    // // }
//    $.ajax({
//        url: 'http://192.168.0.24:8080/action.shtml?id=0001',
//        type: 'get',
//        // data: id='0001',
//        success: function (msg) {
//            alert('shenggong');
//            $("#sections").html("<p>weasfdsf</p>");
//        }
//    })
//};
// 根据actionId获取表格
var tabclick = function (id) {
    var temp = '';
    temp = '' + id;
//        alert(temp);
    $.ajax({
        url: baseUrl+'/action.shtml?id=' + temp,
        type: 'get',
        error: function (data) {
            alert("出错了！！:");
        },
        success: function (msg) {
//                alert('shenggong');
            $("#sections").html(msg);
        },
    });
};
//根据类型classification获取表格
var classclick = function (name) {
    //alert(name);
    $.ajax({
        url: baseUrl+'/group_action.shtml?name=' + name,
        type: 'get',
        error: function (data) {
            alert("ERROR！");
        },
        success: function (msg) {
            $("#sections").html(msg);
        },
    });
};
//input 框的 keyup事件
var invalue = [];
var li = $('nav').children('ul').children('li');
var a = $(li).children(a);
$('input').keyup(function () {
    var geduo = jQuery.trim($(this).val());
    invalue = [];
    invalue = search_by_key(geduo)
    return invalue;
});
//搜索按钮
var search_click = function(btn_name){
    if(btn_name==undefined){
        alert("没有匹配项!");
    }else if (btn_name != 0) {
        classclick(btn_name[0]);
    }else{
        alert("没有匹配项!");
    }
};
//关键字点击查询
var keyword_click = function(btn_name){
    $("#search_input").val(btn_name);
    var classic_name=search_by_key(btn_name);
    if (classic_name != 0) {
        classclick(classic_name[0]);
    }
};

// 关键字匹配公用方法
var search_by_key = function (keyvalue) {

    invalue = [];
    var judge = false;
    for (var i = 0; i < $(li).children(a).length; i++) {
        if ($(li).eq(i).children(a).first().text().indexOf(keyvalue) >= 0) {
            invalue.push($(li).eq(i).attr('id'));
            judge=true;
        }
    }
    if (judge) {
        return invalue;
    }
    return 0;
};

$("#btn").click(function(){
    $('.model').show();
    $('.model2').show(1000);
})
$(".butt2").click(function(){
    $(".model").hide(1000);
    $(".model2").hide();
    $("input[name]='classification_name'").val("");
})
var success = function(){
    $(".success").show();
    window.location.reload();
}
var error = function(){
    $(".error").show();
}
$(".con").click(function(){
    $(".model").hide(1000);
    $(".success").hide();
    $(".error").hide();
})
$(".butt").click(function(){
    var classification_name = $("input[name]='classification_name'").val();
    $(".model2").hide();
    $.ajax({
        url: baseUrl+'/addClassification.shtml',
        type: 'get',
        data:{ 'classification_name': classification_name},
        success: function () {
            success();
            $("input[name]='classification_name'").val("");
        },
        error: function(data){
            error();
            $("input[name]='classification_name'").val("");
        }
    })
})

$(".fl").click(function(){
    action_id = $(this).parent().next().children().attr('id');
    window.location = baseUrl+"/toUpdate.shtml?action_id="+action_id;
    //$.ajax({
    //    url: 'http://192.168.0.24:8080/toUpdate.shtml',
    //    type: 'GET',
    //    data: {'action_id': action_id},
    //    success:function(ds){
    //        $('body').html(ds);
    //    },
    //    error:function(){
    //        alert('失败');
    //    },
    //})
})